﻿@namespace Gurux.DLMS.AMI.Components.Table
@using Gurux.DLMS.AMI.Client.Helpers;
@using Gurux.DLMS.AMI.Components.Enums

@inherits ComponentBase
@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IGXNotifier Notifier


@if (!_hidden)
{
    <td style="@CssStyle" class="@GetCssClass()" @attributes="@AdditionalAttributes">
        @if (Parent != null && Parent.CanEdit)
        {
            <a href="@Link" onclick="@(() => OnSelect())">
                @ChildContent
            </a>
        }
        else
        {
            @ChildContent
        }
    </td>
}

@code
{
    /// <summary>
    /// Id.
    /// </summary>
    [Parameter]
    public string? Id { get; set; }

    /// <summary>
    /// Parent table.
    /// </summary>
    [CascadingParameter]
    private IGXTable? Parent { get; set; }


    private bool CanEdit
    {
        get
        {
            if (Parent == null)
            {
                return true;
            }
            return Parent.CanEdit;
        }
    }

    /// <summary>
    /// href link.
    /// </summary>
    [Parameter]
    public string? Link { get; set; }

    [Parameter]
    public object? Target { get; set; }

    /// <summary>
    /// Gets or sets the item template for the list.
    /// </summary>
    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    /// <summary>
    /// CSS style.
    /// </summary>
    [Parameter]
    public string? CssStyle { get; set; }

    /// <summary>
    /// CSS class.
    /// </summary>
    [Parameter]
    public string? CssClass { get; set; }

    /// <summary>
    /// Responsive determines the order
    /// in which the contents are hidden if they do not fit the screen.
    /// </summary>
    [Parameter]
    public Visibility Visibility { get; set; } = Visibility.All;

    /// <summary>
    /// Is hidden.
    /// </summary>
    bool _hidden;

    private string? GetCssClass()
    {
        return Th.GetCssClass(Visibility, CssClass);
    }

    /// <summary>
    /// Additional attributes.
    /// </summary>
    [Parameter(CaptureUnmatchedValues = true)]
    public IReadOnlyDictionary<string, object>? AdditionalAttributes { get; set; }

    /// <summary>
    /// New target is selected.
    /// </summary>
    private void OnSelect()
    {
        if (Target != null)
        {
            Parent?.SelectCell(Target);
        }
        if (Notifier != null)
        {
            Notifier.ChangePage(NavigationManager.Uri);
        }
        if (!string.IsNullOrEmpty(Link))
        {
            GXHelpers.NavigateTo(NavigationManager, Notifier, Link);
        }
    }

    /// <summary>
    /// Initialize context menu.
    /// </summary>
    protected override void OnInitialized()
    {
        if (Parent == null)
        {
            throw new ArgumentNullException(nameof(Parent), "Th must exist within a GXTable");
        }
        _hidden = Parent.IsHidden(Id);
        base.OnInitialized();
    }
}